package de.java.shortys.myhome.api.view;

import static de.java.shortys.myhome.viewconfigurator.configuration.ViewConfigurationConstants.SWINGSUBMODULEVIEW_BACKGROUND;
import static de.java.shortys.myhome.viewconfigurator.configuration.ViewConfigurationConstants.SWINGSUBMODULEVIEW_BORDER;
import static de.java.shortys.myhome.viewconfigurator.controller.ViewConfigurationManager.getInstance;
import static java.awt.BorderLayout.NORTH;
import static javax.swing.BorderFactory.createCompoundBorder;
import static javax.swing.BorderFactory.createEmptyBorder;
import static javax.swing.BorderFactory.createLineBorder;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.Border;

public abstract class AbstractSwingSubModuleView implements IView {

	private SwingSubModuleHead head;

	private JPanel component;

	private JPanel contentPane;

	private JPanel userContentPane;

	public AbstractSwingSubModuleView() {
		initialze();
	}

	public void setDeactivatable(boolean deactivatable) {
		head.setDeactivatable(deactivatable);
	}

	public void setTitle(String title) {
		head.setTitle(title);
	}

	@Override
	public Component build() {
		return component;
	}

	public void addCloseListener(ActionListener listener) {
		head.addCloseListener(listener);
	}

	public String getTitle() {
		return head.getTitle();
	}

	private void createComponent() {
		component = new JPanel();
		component.setOpaque(false);
		component.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		component.setLayout(new BorderLayout());
	}

	private void createContentPane() {
		contentPane = new JPanel();
		contentPane.setBackground(getInstance().getColor(SWINGSUBMODULEVIEW_BACKGROUND));
		contentPane.setLayout(new BorderLayout());
		Border lineBorder = createLineBorder(getInstance().getColor(SWINGSUBMODULEVIEW_BORDER));
		Border paddingBorder = createEmptyBorder(1, 1, 1, 1);
		contentPane.setBorder(createCompoundBorder(lineBorder, paddingBorder));
		component.add(contentPane);
	}

	public JPanel getContentArea() {
		return userContentPane;
	}

	private void initialze() {
		createComponent();
		createContentPane();
		createHead();
		createUserContentPane();
	}

	private void createUserContentPane() {
		userContentPane = new JPanel();
		userContentPane.setLayout(new BorderLayout());
		userContentPane.setOpaque(false);
		contentPane.add(userContentPane);
	}

	private void createHead() {
		head = new SwingSubModuleHead();
		contentPane.add(NORTH, head);
	}

	public abstract void clear();
}
